package com.wuxl.prada;

/**
 * Description:
 *
 * @author 诸葛小猿
 * @date 2024-12-19
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SrtParser {
    public static List<Subtitle> parse(String filePath) throws IOException {
        List<Subtitle> subtitles = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (line.trim().isEmpty()) continue;
                int index = Integer.parseInt(line);
                String timeRange = br.readLine();
                String[] parts = timeRange.split(" --> ");
                String startTime = parts[0].split(",")[0];
                String endTime = parts[1].split(",")[0];
                StringBuilder text = new StringBuilder();
                while ((line = br.readLine()) != null && !line.trim().isEmpty()) {
                    line = line.replaceAll("</i>","").replaceAll("<i>","").replaceAll("[^a-zA-Z\\s]","")
                    ;
//                    System.out.println(line);
                    text.append(line).append("\n");
                }
                subtitles.add(new Subtitle(index, startTime, endTime, text.toString().trim()));
            }
        }
        return subtitles;
    }

    public static class Subtitle {
        private final int index;
        private final String startTime;
        private final String endTime;
        private final String text;

        public Subtitle(int index, String startTime, String endTime, String text) {
            this.index = index;
            this.startTime = startTime;
            this.endTime = endTime;
            this.text = text;
        }

        public int getIndex() {
            return index;
        }

        public String getStartTime() {
            return startTime;
        }

        public String getEndTime() {
            return endTime;
        }

        public String getText() {
            return text;
        }
    }
}
